package com.project.controller.web;

import com.alibaba.fastjson.JSONObject;
import com.project.entity.School;
import com.project.myBeans.R;
import com.project.myBeans.ResponseBean;
import com.project.service.SchoolManagerInfoService;
import com.project.service.SchoolService;
import com.project.service.base.BaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: TateBrown
 * @date: 2018/5/27 16:54
 * @param:
 * @return:
 */
@EnableAutoConfiguration //自动载入应用程序所需的所有Bean
@RestController //@Controller与@ResponseBody的合并
@RequestMapping(value = "/school")
@Api(description = "学院相关",value = "school")
public class SchoolController  extends BaseService {
    @Autowired
    SchoolService schoolService;

    @Autowired
    private SchoolManagerInfoService schoolManagerInfoService;

    /**
     * TateBrown 2018年5月27日
     *
     * @param universityId
     * @return
     */
    @ApiOperation("根据学校id查找学院")
    @GetMapping("/findBath/{universityId}")
    public R findBath(@PathVariable("universityId") Integer universityId) {
        List<School> schoolList;
        try {
            schoolList = schoolService.findBatchByUniversityId(universityId);
            return R.ok("查询成功").put("data",schoolList);
        } catch (Exception e) {
            return R.error();
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param universityId
     * @return
     */
    @ApiOperation("根据学校id查找学院以及学院的管理员")
    @GetMapping("/findBatchByUniversityIdWithAdmin/{universityId}")
    public R findBatchByUniversityIdWithAdmin(@PathVariable("universityId") Integer universityId){
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<School> schoolList;
        try {
            schoolList = schoolService.findBatchByUniversityId(universityId);
            for (School school: schoolList){
                JSONObject jsonObject = new JSONObject();
                //学院信息
                jsonObject.put("school", school);
                //学院管理员信息
                if (school.getId() != null)
                    jsonObject.put("schoolManagerInfo",schoolManagerInfoService.findOneBySchoolId(school.getId()));
                jsonObjectList.add(jsonObject);
            }
           return R.ok("查询成功").put("data",jsonObjectList);
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param school
     * @return
     */
    @ApiOperation("添加学院")
    @PostMapping("/addOne")
    public R addOne(@RequestBody School school) {
        try {
            //查看学院是否已存在
            if (schoolService.checkUniversityIdAndNameIsExist(school)) {
                return R.error("已存在，创建失败");
            } else {
                schoolService.add(school);
               return R.ok("创建成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
         return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param school
     * @return
     */
    @ApiOperation("修改单条学院表记录")
    @PostMapping("/modifyOneById")
    public R modifyOneById(@RequestBody School school){

        try {
            if (checkDeleteUpdateIsSuccess(schoolService.modify(school))){
                return R.ok("修改成功");
            }
            else{
             return R.error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id查看单条")
    @GetMapping("/findOneById/{id}")
    public R findOneById(@PathVariable("id") Integer id){
        try {
            return R.ok("获取成功").put("data",schoolService.findOne(id));

        } catch (Exception e) {
            e.printStackTrace();
          return R.error("程序异常");
        }
    }
    /**
     * TateBrown 2018年5月27日
     *
     * @param schoolId
     * @return
     */
    @ApiOperation("根据id级联删除相关x学院下所有")
    @GetMapping("/removeOneChain/{schoolid}")
    public R removeOneChain(@PathVariable("schoolid") Integer schoolId, HttpServletRequest request){
        try {
            schoolService.removeOneChain(schoolId, request);
            return R.ok("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
          return R.error("程序异常");
        }
    }
}
